package com.future.mayi.utils;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.utils.SealConst;
import com.future.baselib.utils.ToastUtil;
import com.future.baselib.view.CircleImageView;
import com.future.mayi.R;
import com.future.mayi.adapter.BaseRecyclerViewAdapter;
import com.future.mayi.app.App;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import java.util.List;


/**
 * Created by 周大虎 on 2017/2/7.
 */
public class HCUtils {


    /**
     * 是网络连接
     *
     * @return
     */
    public static boolean isNetworkConnected(Context mContext) {
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnectedOrConnecting();
    }


    public static <T> void refreshListForPage(XRecyclerView recyclerView, BaseRecyclerViewAdapter adapter, List<T> list, int page, int pageSize) {
        if (recyclerView == null || adapter == null)
            return;

        if (list == null || list.size() == 0) {
            if (page == 0)
                adapter.setList(null);
            recyclerView.setNoMore(true);
            return;
        }

        if (page > 0) {
            adapter.addMore(list);
            recyclerView.loadMoreComplete();
        } else {
            adapter.setList(list);
            recyclerView.refreshComplete();
        }


    }

    public static <T> void refreshList(XRecyclerView recyclerView, BaseRecyclerViewAdapter adapter, List<T> list) {
        if (recyclerView == null || adapter == null)
            return;


        adapter.setList(list);
        recyclerView.refreshComplete();
        recyclerView.setNoMore(true);


    }


    /**
     * 设置刷新失败之后结束动画
     */
    public static int refreshFail(XRecyclerView recyclerView, int page, Context context, BaseResponse response) {
        String message = null;
        if (response != null)
            message = response.message;


        if (recyclerView != null) {
            if (page == 0) {
                recyclerView.refreshComplete();
            } else {
                recyclerView.loadMoreComplete();
            }
        }

        if (context != null)
            loadFail(context, message);
        return (page > 0) ? page - 1 : page;
    }


    /**
     * 设置刷新失败之后结束动画
     */
    public static int refreshFail(XRecyclerView recyclerView, int page, Context context, String message) {
        if (recyclerView != null) {
            if (page == 0) {
                recyclerView.refreshComplete();
            } else {
                recyclerView.loadMoreComplete();
            }
        }

        if (context != null)
            loadFail(context, message);
        return (page > 0) ? page - 1 : page;
    }


    public static void loadFail(Context context, String message) {
        if (TextUtils.isEmpty(message))
            return;
        ToastUtil.getInstance().show(context, message);
        if (TextUtils.equals(message, "请重新登录")) {
            BroadcastManagerUtil.getInstance(context).sendBroadcast(SealConst.EXIT);
        } else if (TextUtils.equals(message, "账号被禁用")) {
            BroadcastManagerUtil.getInstance(context).sendBroadcast(SealConst.BAN);
        }
    }


    /**
     * 判断网络是否连接
     *
     * @param context
     * @return
     */
    public static boolean isConnected(Context context) {

        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (null != connectivity) {

            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (null != info && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 判断是否是wifi连接
     */
    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm == null)
            return false;
        return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;

    }

    /**
     * 打开网络设置界面
     */
    public static void openSetting(Activity activity) {
        Intent intent = new Intent("/");
        ComponentName cm = new ComponentName("com.android.settings",
                "com.android.settings.WirelessSettings");
        intent.setComponent(cm);
        intent.setAction("android.intent.action.VIEW");
        activity.startActivityForResult(intent, 0);
    }

    /**
     * 加载网络图
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void loadWebImg(Context context, ImageView imageView, String url) {
        if (imageView == null)
            return;
        if (context == null) return;
        if (context instanceof Activity && ((Activity) context).isFinishing()) return;
        if (imageView.getTag(R.id.user_head_img) != null) {
            Glide.with(App.getmContext()).load(url).placeholder(R.drawable.img_default_head).error(R.drawable.img_default_head).dontAnimate().into(imageView);
        } else {
            Glide.with(App.getmContext()).load(url).placeholder(R.color.transparent).error(R.color.black4).into(imageView);
        }
    }

    public static void loadWebImg(Context context, ImageView imageView, String url, int resId) {
        if (imageView == null)
            return;
        if (context == null) return;
        if (context instanceof Activity && ((Activity) context).isFinishing()) return;
        Glide.with(App.getmContext()).load(url).placeholder(R.color.transparent).error(resId).into(imageView);
    }


    public static void loadWebImg(Context context, ImageView imageView, int resId) {
        if (context == null) return;
        if (context instanceof Activity && ((Activity) context).isFinishing()) return;
        Glide.with(App.getmContext()).load(resId).placeholder(R.color.transparent).error(R.color.black4).into(imageView);
    }


}
